package com.demo.controller;

import com.demo.base.BaseController;
import com.demo.domain.User;
import com.demo.service.UserService;
import com.demo.tools.MD5Encrypt;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author：涛
 * @Descripition：
 * @Date：2018/3/19 22:41
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;
    
    @RequestMapping(value = "/getLogin", method = RequestMethod.GET)
    public String getLogin() throws Exception{
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@Param("username") String username, @Param("password") String password, Model view) throws Exception {
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            view.addAttribute("error", "用户名或密码不能为空!");
            return "login";
        }
        MD5Encrypt md5 = new MD5Encrypt();
        password = md5.getMD5ofStr(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        List<User> list = userService.selectByCondition(user);
        if(CollectionUtils.isEmpty(list)) {
            view.addAttribute("error", "用户名或密码错误!");
            return "login";
        }
        view.addAttribute("username", list.get(0).getUsername());
        return "home";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(){
        return "index";
    }

    @RequestMapping(value = "/getRegister", method = RequestMethod.GET)
    public String getRegister() {
        return "register";
    }
    
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(Model view, @Validated User user) throws Exception {
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            view.addAttribute("error", "用户名或密码不能为空!");
            return "register";
        }
        String password = user.getPassword();
        MD5Encrypt md5 = new MD5Encrypt();
        password = md5.getMD5ofStr(password);
        user.setPassword(password);
        userService.insertEntry(user);
        view.addAttribute("username", user.getUsername());
        return "home";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        //文件原始名称
        String originalFilename = file.getOriginalFilename();
        //上传文件
        if(file != null && originalFilename != null && originalFilename.length() > 0){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //存储文件的物理路径
            String filePath = "D:/Program Files/Apache Software Foundation/apache-tomcat-7.0.82/upload/images/" + simpleDateFormat.format(new Date());
            //判断文件目录是否存在，若不存在，则创建
            if(!new File(filePath).exists()){
                new File(filePath).mkdirs();
            }
            filePath = filePath + "\\";
            //新文件名字
            String newFilename = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            //新文件
            File newFile = new File(filePath + newFilename);
            //将内存中的数据写入磁盘
            file.transferTo(newFile);
            return "uploadSuccess";
        }
        return "uploadError";
    }
}
